`:top
In computer graphics programming, `!hit-testing`! (`!hit detection`!, `!picking`!, or `!pick correlation`!`:cite-ref-foley-1-0[`F5bf`_`[1`#cite-note-foley-1]`_`f]) is the process of determining whether a user-controlled cursor (such as a mouse cursor or touch-point on a touch-screen interface) intersects a given graphical object (such as a shape, line, or curve) drawn on the screen. Hit-testing may be performed on the movement or activation of a mouse or other pointing device.
Hit-testing is used by `F33f`_`[GUI`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GUI]`_`f environments to respond to user actions, such as selecting a menu item or a target in a game based on its visual location. In web programming languages such as `F33f`_`[HTML`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=HTML]`_`f, `F33f`_`[SVG`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scalable_Vector_Graphics]`_`f, and `F33f`_`[CSS`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=CSS]`_`f, this is associated with the concept of pointer-events (e.g. user-initiated cursor movement or object selection).
`F33f`_`[Collision detection`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Collision_detection]`_`f is a related concept for detecting intersections of two or more different graphical objects, rather than intersection of a cursor with one or more graphical objects.
>>Contents
• `F0af`_`[Algorithm`#algorithm]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>Algorithm
There are many different algorithms that may be used to perform hit-testing, with different performance or accuracy outcomes. One common hit-test algorithm for axis aligned bounding boxes. A key idea is that the box being tested must be either entirely above, entirely below, entirely to the right or left of the current box. If this is not possible, they are colliding. Example logic is presented in the pseudo-code below:
`B100`F9d9function HitTest(Rectangle r1, Rectangle r2) returns boolean`f`b
`B100`F9d9{`f`b
`B100`F9d9 return not((r1.X + r1.Width < r2.X) or (r1.X > r2.X + r2.Width) or (r1.Y + r1.Height < r2.Y) or (r1.Y > r2.Y + r2.Height));`f`b
`B100`F9d9}`f`b
In `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f:
`B100`F9d9def hit_test(r1: Rectangle, r2: Rectangle) -> bool:`f`b
`B100`F9d9 """Return true if it hits else return false."""`f`b
`B100`F9d9 return (`f`b
`B100`F9d9 not (r1.x + r1.width < r2.x)`f`b
`B100`F9d9 or (r1.x > r2.x + r2.width)`f`b
`B100`F9d9 or (r1.y + r1.Height < r2.y)`f`b
`B100`F9d9 or (r1.y > r2.y + r2.height)`f`b
`B100`F9d9 )`f`b
>>See also
• `F33f`_`[Point in polygon`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Point_in_polygon]`_`f
• `F33f`_`[Computational geometry`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computational_geometry]`_`f
• `F33f`_`[Collision detection`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Collision_detection]`_`f
• `F33f`_`[User interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=User_interface]`_`f
>>References
`:cite-note-foley-1`!1.`! `F0af`_`[↑`#cite-ref-foley-1-0]`_`f `F33f`_`[Computer Graphics: Principles and Practice`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_Graphics:_Principles_and_Practice]`_`f 2nd Edition in C, Foley et al, `F33f`_`[Addison-Wesley`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Addison-Wesley]`_`f, 1997.
>>External links
• MSDN: Hit Testing in the Visual Layer
• MSDN: Hit Testing Lines and Curves
`c`F0af`_`[↑ Back to top`#top]`_`f`a